<?php

namespace App\Http\Controllers;

use App\Exports\PhoneRecordExport;
use App\Models\PhoneRecord;
use App\Transformers\PhoneRecordTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\StatisticPhoneRecord;
use App\Models\StatisticSmsRecord;

class PhoneRecordController extends Controller
{
    /**
     * @api {get} /api/record?name=XXX&page=1&limit=2&include=user,fsUser 所有录音列表
     * @apiGroup PhoneRecord
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} search  搜索条件
     * @apiParam {String} name  电销人员账号
     * @apiParam {String} username  会员用户名
     * @apiParam {String} true_name  会员真名
     * @apiParam {int} start_recording_time  通话时长开始
     * @apiParam {int} end_recording_time  通话时长结束
     * @apiParam {datetime} created_time  通话时间范围开始
     * @apiParam {datetime} updated_time  通话时间范围结束
     * @apiParam {int} page   当前页数
     * @apiParam {int} limit  返回总条数
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "user": "admin",
     *                   "username": "",
     *                   "true_name": "",
     *                   "call_status": null,
     *                   "recording_url": "",
     *                   "recording_time": "",
     *                   "start_time": "",
     *                   "end_time": ""
     *               },
     *           ]
     *       }
     */
    public function index(Request $request,PhoneRecord $phoneRecord){
        //电销人员账号（user表）
        $name = $request->input('name','');
        //会员用户名 （fs_user）
        $username = $request->input('username','');
        //会员真名
        $true_name = $request->input('true_name','');
        //通话时长
        $start_recording_time = $request->input('start_recording_time','');
        $end_recording_time = $request->input('end_recording_time','');
        //通话时间范围
        $created_at_time = $request->input('created_time','');
        $updated_at_time = $request->input('updated_time','');
        $limit = $request->input('limit',10);

        $records= $phoneRecord->search($name,$username,$true_name,$start_recording_time,$end_recording_time,$created_at_time,$updated_at_time,$limit);

        return $this->response->paginator($records, new PhoneRecordTransformer());
    }
    /**
     * @api {get} /api/record/current?name=XXX&page=1&limit=2&include=user,fsUser 当前登陆者的录音列表
     * @apiGroup PhoneRecord
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} search  搜索条件
     * @apiParam {String} name  电销人员账号
     * @apiParam {String} username  会员用户名
     * @apiParam {String} true_name  会员真名
     * @apiParam {int} start_recording_time  通话时长开始
     * @apiParam {int} end_recording_time  通话时长结束
     * @apiParam {datetime} created_time  通话时间范围开始
     * @apiParam {datetime} updated_time  通话时间范围结束
     * @apiParam {int} page   当前页数
     * @apiParam {int} limit  返回总条数
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "user": "admin",
     *                   "username": "",
     *                   "true_name": "",
     *                   "call_status": null,
     *                   "recording_url": "",
     *                   "recording_time": "",
     *                   "start_time": "",
     *                   "end_time": ""
     *               },
     *           ]
     *       }
     */
    public function currentIndex(Request $request,PhoneRecord $phoneRecord){
       $user_id = Auth::id();
        //电销人员账号（user表）
        $name = $request->input('name','');
        //会员用户名 （fs_user）
        $username = $request->input('username','');
        //会员真名
        $true_name = $request->input('true_name','');
        //通话时长
        $start_recording_time = $request->input('start_recording_time','');
        $end_recording_time = $request->input('end_recording_time','');
        //通话时间范围
        $created_at_time = $request->input('created_time','');
        $updated_at_time = $request->input('updated_time','');
        $limit = $request->input('limit',10);

        $records= $phoneRecord->search($name,$username,$true_name,$start_recording_time,$end_recording_time,$created_at_time,$updated_at_time,$limit,'','');

        return $this->response->paginator($records, new PhoneRecordTransformer());
    }

    /**
     * @api {get} /api/record/export?token=access_token 电话记录导出
     * @apiGroup PhoneRecord
     *
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} search  搜索条件
     * @apiParam {String} name  电销人员账号
     * @apiParam {String} username  会员用户名
     * @apiParam {String} true_name  会员真名
     * @apiParam {int} start_recording_time  通话时长开始
     * @apiParam {int} end_recording_time  通话时长结束
     * @apiParam {datetime} created_time  通话时间范围开始
     * @apiParam {datetime} updated_time  通话时间范围结束
     *
     * @apiParam {int} page   当前页数
     * @apiParam {int} limit  返回总条数
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *
     **/
    public function export(Request $request, PhoneRecordExport $fsUserExport)
    {
        set_time_limit(0);
        ini_set('memory_limit', -1);
        $data = $request->all();
        return $fsUserExport->condition($data);
    }

    public function count_number(StatisticPhoneRecord $statisticSmsRecord)
    {
        dd($statisticSmsRecord->count());
    }

}
